require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#myCoupon",
    data:{
        name:"",
        type:"",
        loadFlag:false,
        value:1,
        list:{
            loginUid:userInfo.id,
            state:1,
            pageNumber:1
        },
        // isScreen:"",
        noticeList:{},//列表
        noticeList1:[],
        noticeList2:[],
        pop:{
            question: "确认要删除吗? ",
            show: false
        },
        couponId:"",
        newCoupon:{}
    },
    mounted:function(){
        // this.searchObject = this.getQueryStringArgs();
        // this.type=this.searchObject&&this.searchObject.type;
        // if(this.type){
        //     if(this.type==5){
        //         this.name="关注";
        //     }else if(this.type==6){
        //         this.name="粉丝";
        //     }
        //     this.myFansList();
        // }
        this.getMyCoupon();
        this.myQualificationCoupon();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //tab切换
        getTab:function(num){
            this.newCoupon={};
            this.noticeList={};
            this.noticeList1=[];
            this.noticeList2=[];
            this.list.state=num;
            this.list.pageNumber=1;
            this.getMyCoupon();
            this.myQualificationCoupon();
        },
        //我的优惠券列表
        getMyCoupon:function(){
            var that=this;
            this.ajax({url:"/happyi/getMyCoupon",data:that.list,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.noticeList=b.json.data;
                    that.noticeList1=that.noticeList.list;
                    if(that.noticeList.pageNumber>1){
                        if(that.noticeList.list.length>0) {
                            that.noticeList2 = that.noticeList2.concat(b.json.data.list);
                        }
                    }else{
                        that.noticeList2=that.noticeList1;
                    }
                    console.log(that.noticeList2)

                    vm.loadFlag=false;
                }else{
                    that.noticeList={};
                    that.noticeList1=[];
                    that.noticeList2=[];
                    vm.$refs.prompt.toast("获取列表失败！");
                }
            });
        },
        //我的新人资格优惠券
        myQualificationCoupon:function(){
            var that=this,data;
            data={
                loginUid:that.list.loginUid,
                state:that.list.state
            };
            console.log(data)
            this.ajax({url:"/happyi/myQualificationCoupon",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.newCoupon=b.json.data;
                    if(that.newCoupon){
                        that.newCoupon.beginDate=that.newCoupon.beginDate.split("-").join(".");
                        that.newCoupon.endDate=that.newCoupon.endDate.split("-").join(".");
                    }
                    // that.noticeList=b.json.data;
                    // that.noticeList1=that.noticeList.list;
                    // if(that.noticeList.pageNumber>1){
                    //     if(that.noticeList.list.length>0) {
                    //         that.noticeList2 = that.noticeList2.concat(b.json.data.list);
                    //     }
                    // }else{
                    //     that.noticeList2=that.noticeList1;
                    // }
                    // console.log(that.noticeList2)
                    // vm.loadFlag=false;
                }else{
                    // that.noticeList={};
                    // that.noticeList1=[];
                    // that.noticeList2=[];
                    vm.$refs.prompt.toast("获取优惠券失败！");
                }
            });

        },
        getIsClassify:function(couponId){
            this.couponId=couponId;
            this.pop.show=!this.pop.show;
        },
        //删除优惠券
        deleteCoupon:function(){
            var that=this;
            var data={
                loginUid:that.list.loginUid,
                couponId:that.couponId
            };
            console.log(data);
            that.getIsClassify();
            this.ajax({url:"/friend/deleteCoupon",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getMyCoupon();
                }else{
                    vm.$refs.prompt.toast("删除帖子失败！");
                }
            });
        },
        getNext:function(type,item){
            if(type==1){
                location.href="/purchase/newlyweds";
            }else if(type==2){
                location.href="/enjoy";
            }else if(type==3){
                console.log(item)
                location.href="/purchase/goodsDetails?type=1&productId="+item.productId;
            }
        }
    }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag = vm.noticeList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.list.pageNumber++;
            vm.getMyCoupon();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};